<?php
require_once APPLICATION_PATH . "/models/ModelBase.php";

class Tasks extends TT_Table_Abstract
{
    protected $_name = 'users';
    protected $_primary = 'uid';
    
    // Lay uid trong users_info
    public function checkInfo($uid)    
    {
        $db = $this->getSessionDB();
        $sql = 'SELECT * FROM users_info WHERE uid=? ';
        return $db->fetchRow($sql, array($uid));         
    }
    public function checkInfo2($username)                   // kiem tra user co ton tai ko
            {
               $db = $this->getSessionDB();
                $sql = 'SELECT * FROM users WHERE name=? ';
                return $db->fetchRow($sql, array($username));         
            }
    // Tao moi bao cao
    public function insert_new_task($creator_id,$task_title,$date_start,$date_end,$status,$degree_complete,$description)
    {
        $updated=time();
        $db = $this->getSessionDB();    
        $sql ="INSERT INTO tasks ( creator_id , task_title , date_start , date_end ,status ,degree_complete,description,updated,del_temporary) VALUES ( ?,?,?,?,?,?,?,$updated,0) ";
        return $db->query($sql,array($creator_id,$task_title,$date_start,$date_end,$status,$degree_complete,$description));
    }

    // Phan trang
    public function view_my_task($uid,$page,$page_rows)   //xem nhung bao cao cua ca nhan

        {
            $db = $this->getSessionDB();
            $sql="SELECT * FROM tasks WHERE creator_id=? AND del_temporary=0 ORDER BY updated DESC LIMIT $page,$page_rows";
            return $db->fetchAll($sql,array($uid)); 
        }

    public function view_my_task_num($uid)

        {
            $db = $this->getSessionDB();
            $sql="SELECT count(task_id) as n FROM tasks WHERE creator_id=? AND del_temporary=0 ORDER BY date_start DESC";
            return $db->fetchRow($sql,array($uid)); 
        }

    public function task_info($task_id)       // thong tin cua bao cao
    {
        $db = $this->getSessionDB();
        $sql="SELECT * FROM tasks WHERE task_id=?";
        return $db->fetchRow($sql,array($task_id));
    }
    // Sua bao cao
    public function edit_task($task_id,$task_title,$date_start,$date_end,$status,$degree_complete,$description)
        {
            $updated=time();
            $db = $this->getSessionDB();    
            $sql ="UPDATE tasks SET  task_title=? , date_start=? , date_end=? ,status=? ,degree_complete=?,description=?,updated=$updated WHERE task_id =?";
            return $db->query($sql,array($task_title,$date_start,$date_end,$status,$degree_complete,$description,$task_id));
        }
     public function delete_task($task_id)      //xoa
        {
            $db = $this->getSessionDB();    
            $sql ="UPDATE tasks SET  del_temporary=1 WHERE task_id =?";
            return $db->query($sql,array($task_id));
        }
     public function recover_task($task_id)    // phuc hoi
        {
            $db = $this->getSessionDB();    
            $sql ="UPDATE tasks SET  del_temporary=0 WHERE task_id =?";
            return $db->query($sql,array($task_id));
        }
     public function view_recyclebin_task($uid,$page,$page_rows)
        {
            $db = $this->getSessionDB();
            $sql="SELECT * FROM tasks WHERE creator_id=? AND del_temporary=1 ORDER BY updated DESC LIMIT $page,$page_rows";
            return $db->fetchAll($sql,array($uid)); 
        }
     public function view_recyclebin_task_num($uid)
        {
            $db = $this->getSessionDB();
            $sql="SELECT count(task_id) as n FROM tasks WHERE creator_id=? AND del_temporary=1 ORDER BY date_start DESC";
            return $db->fetchRow($sql,array($uid)); 
        }
     public function phong()
        {
            $db = $this->getSessionDB();
            $sql="select phong_ban.under, phong_ban.id,users.uid,phong_ban.phong_ban, users.name as username,users_info.name from users inner join phong_ban inner join users_info on users.uid= phong_ban.id_manager and users.uid=users_info.uid";
            return $db->fetchAll($sql,array($uid));
        }
     public function phong2($phong) //lay thong tin phong_ban khi biet ten phong
        {
            $db = $this->getSessionDB();
            $sql="select * from phong_ban where phong_ban=?";
            return $db->fetchRow($sql,array($phong)); 
        }
     public function editmanager($id,$id_manager)             // thay user lam qquan ly cua phong ban
        {
            $db = $this->getSessionDB();
            $sql ="UPDATE phong_ban SET id_manager = ? WHERE id =?";
            return $db->query($sql,array($id_manager,$id));
        }
     public function update_phong($id,$under)
        {
            $db = $this->getSessionDB();
            $sql ="UPDATE phong_ban SET under = ? WHERE id =?";
            return $db->query($sql,array($under,$id));
        }
     public function listmember_1($id_manager)      // xem danh sach nguoi bao cao truc tiep den 
        {
            $db = $this->getSessionDB();
            $sql = "SELECT phong_ban.phong_ban as phong_ban , users.name as username , users_info.uid ,users_info.name FROM phong_ban inner join users_info inner join users on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=users.uid and phong_ban.id_manager=? and users_info.date_out=0 and users_info.uid!=?"; 
            return $db->fetchAll($sql,array($id_manager,$id_manager));
        }
     public function listmember_2($id_manager)       // xem danh sach nguoibao cao dan tiep den 
        {
            $db = $this->getSessionDB();
            $sql = "select users_info.phong_ban as phong_ban , users.name as username , users_info.uid ,users_info.name from permission inner join users inner join users_info on permission.creator_id=users.uid and permission.creator_id=users_info.uid and permission.viewer_id=? and users_info.date_out=0 and users_info.uid!=? "; 
            return $db->fetchAll($sql,array($id_manager,$id_manager));
        }
     public function listmember($id_manager,$page_start,$page_row)     // xem danh sach nguoibao cao truc tiep den ( co phan trang) 
        {
            $db = $this->getSessionDB();
            $sql = "SELECT phong_ban.phong_ban as phong_ban , users.name as username , users_info.uid ,users_info.name FROM phong_ban inner join users_info inner join users on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=users.uid and phong_ban.id_manager=? and users_info.date_out=0 and users_info.uid!=? limit $page_start,$page_row"; 
            return $db->fetchAll($sql,array($id_manager,$id_manager));
        }
     public function listmember_num($id_manager)        // dem so trang
        {
            $db = $this->getSessionDB();
            $sql = "SELECT count(*) as n FROM phong_ban inner join users_info inner join users on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=users.uid and phong_ban.id_manager=? and users_info.date_out=0 and users_info.uid!=?"; 
            return $db->fetchRow($sql,array($id_manager,$id_manager));
        }
     public function listmember2($id_manager,$page_start,$page_row)       // xem danh sach nguoibao cao dan tiep den 
        {
            $db = $this->getSessionDB();
            $sql = "select users_info.phong_ban as phong_ban , users.name as username , users_info.uid ,users_info.name from permission inner join users inner join users_info on permission.creator_id=users.uid and permission.creator_id=users_info.uid and permission.viewer_id=? and users_info.date_out=0 and users_info.uid!=? limit $page_start,$page_row"; 
            return $db->fetchAll($sql,array($id_manager,$id_manager));
        }
     public function listmember2_num($id_manager) 
        {
            $db = $this->getSessionDB();
            $sql = "select count(*) as n from permission inner join users inner join users_info on permission.creator_id=users.uid and permission.creator_id=users_info.uid and permission.viewer_id=? and users_info.date_out=0 and users_info.uid!=?"; 
            return $db->fetchRow($sql,array($id_manager,$id_manager));
        }
     public function listmember3($phong_ban)  // danh sach nguoi phai bao cao la quan ly cua phong ban truc thuoc
        {
            $db = $this->getSessionDB();
            $sql = "select users_info.phong_ban as phong_ban , users.name as username , users_info.uid ,users_info.name from phong_ban inner join users_info inner join users on phong_ban.id_manager = users_info.uid and users_info.uid=users.uid and phong_ban.under=? "; 
            return $db->fetchAll($sql,array($phong_ban));
        }
     public function findtask($id_manager)
        {
            $db = $this->getSessionDB();
            $sql ="SELECT *,tasks.status,tasks.updated FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=? and tasks.del_temporary=0 ORDER BY tasks.date_start DESC"; 
            return $db->fetchAll($sql,array($id_manager));  
        }
      public function findtask2($id_viewer)
        {
            $db = $this->getSessionDB();
            $sql ="SELECT *,tasks.status,tasks.updated FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.del_temporary=0 ORDER BY tasks.date_start DESC"; 
            return $db->fetchAll($sql,array($id_viewer));  
        }
      public function find_uid_manager($uid)    // tim uid nguoi quan ly truc tiep ..
        {
            $db = $this->getSessionDB();
            $sql ="select phong_ban.id_manager,phong_ban.phong_ban from phong_ban inner join users_info on phong_ban.phong_ban=users_info.phong_ban where users_info.uid=?"; 
            return $db->fetchRow($sql,array($uid));  
        }
      public function find_viewer($creator_id) // tim nguoi dc xem 
        {
            $db = $this->getSessionDB();
            $sql ="select * from permission inner join users_info on viewer_id=uid where creator_id=?";
            return $db->fetchAll($sql,array($creator_id));    
        }
      public function delete_viewer($creator_id,$viewer_id)       // ko cho xem bao cua nua
        {   
            $db = $this->getSessionDB();
            $sql ="delete from permission where creator_id=? and viewer_id=?";
            return $db->query($sql,array($creator_id,$viewer_id));     
        }
      public function add_viewer($creator_id,$viewer_id)        // them 1 nguoi duoc xem bao cao
        {   
            $db = $this->getSessionDB();
            $sql ="insert into permission (creator_id,viewer_id) values (?,?)";
            return $db->query($sql,array($creator_id,$viewer_id));     
        }
      public function find_manager($uid)    // tim nguoi quan lu cua phong ban truc thuoc
        {
            $db = $this->getSessionDB();
            $sql ="select * from phong_ban where id_manager=?";
            return $db->fetchRow($sql,array($uid));
        }
 /*     public function viewtask($id_manager,$creator_id,$date1,$date2)        // lay bao cao truc tiep cua 1 user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="SELECT *,tasks.status,tasks.updated FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=? and tasks.creator_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC"; 
            return $db->fetchAll($sql,array($id_manager,$creator_id,$date1,$date2));   
        }
      public function viewalltask($id_manager,$date1,$date2)             // lay bao cao truc tiep cua tat ca user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="SELECT *,tasks.status,tasks.updated FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC"; 
            return $db->fetchAll($sql,array($id_manager,$date1,$date2));   
        } */
      public function viewtask_paging($id_manager,$creator_id,$date1,$date2,$page_start,$page_row)        // lay bao cao truc tiep cua 1 user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="SELECT *,tasks.status,tasks.updated FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=? and tasks.creator_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC limit $page_start,$page_row"; 
            return $db->fetchAll($sql,array($id_manager,$creator_id,$date1,$date2));   
        }
      public function viewtask_num($id_manager,$creator_id,$date1,$date2)        // lay bao cao truc tiep cua 1 user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="SELECT count(*) as n FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=? and tasks.creator_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC"; 
            return $db->fetchRow($sql,array($id_manager,$creator_id,$date1,$date2));   
        }
      public function viewalltask_paging($id_manager,$date1,$date2,$page_start,$page_row)             // lay bao cao truc tiep cua tat ca user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="SELECT *,tasks.status,tasks.updated FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC limit $page_start,$page_row"; 
            return $db->fetchAll($sql,array($id_manager,$date1,$date2));   
        }
      public function viewalltask_num($id_manager,$date1,$date2)             // dem so bao cao truc tiep cua tat ca user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="SELECT count(*) as n FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC"; 
            return $db->fetchRow($sql,array($id_manager,$date1,$date2));   
        }
  /*    public function viewtask2($id_viewer,$creator_id,$date3,$date4)             // lay bao cao dan tiep cua tat ca user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="SELECT *,tasks.status,tasks.updated FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.creator_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC"; 
            return $db->fetchAll($sql,array($id_viewer,$creator_id,$date3,$date4)); 
        } */
      public function viewtask2_num($id_viewer,$creator_id,$date3,$date4)             // lay bao cao dan tiep cua tat ca user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="SELECT count(*) as n FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.creator_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC"; 
            return $db->fetchRow($sql,array($id_viewer,$creator_id,$date3,$date4)); 
        } 
      public function viewtask2_paging($id_viewer,$creator_id,$date3,$date4,$page_start,$page_row)             // lay bao cao dan tiep cua tat ca user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="SELECT *,tasks.status,tasks.updated FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.creator_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC limit $page_start,$page_row"; 
            return $db->fetchAll($sql,array($id_viewer,$creator_id,$date3,$date4)); 
        }
  /*    public function viewalltask2($id_viewer,$date3,$date4)        // lay bao cao dan tiep (cua nguoi duoc xem) cua 1 user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="SELECT *,tasks.status,tasks.updated FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC"; 
            return $db->fetchAll($sql,array($id_viewer,$date3,$date4));   
        } */
      public function viewalltask2_paging($id_viewer,$date3,$date4,$page_start,$page_row)        // lay bao cao dan tiep (cua nguoi duoc xem) cua 1 user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="SELECT *,tasks.status,tasks.updated FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC limit $page_start,$page_row"; 
            return $db->fetchAll($sql,array($id_viewer,$date3,$date4));   
        }
      public function viewalltask2_num($id_viewer,$date3,$date4)        // lay bao cao dan tiep (cua nguoi duoc xem) cua 1 user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="SELECT count(*) as n FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC"; 
            return $db->fetchRow($sql,array($id_viewer,$date3,$date4));   
        }
  /*    public function viewtask3($phong_ban,$creator_id,$date3,$date4)             // lay bao cao dan tiep cua tat ca quan ly phong truc thuoc  trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="select *,tasks.status,tasks.updated from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid and tasks.del_temporary=0 and  phong_ban.under=? and tasks.creator_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC"; 
            return $db->fetchAll($sql,array($phong_ban,$creator_id,$date3,$date4)); 
        } */
        
      public function viewtask3_num($phong_ban,$creator_id,$date3,$date4)             // lay bao cao dan tiep cua tat ca quan ly phong truc thuoc  trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="select count(*) as n from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid and tasks.del_temporary=0 and  phong_ban.under=? and tasks.creator_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC"; 
            return $db->fetchRow($sql,array($phong_ban,$creator_id,$date3,$date4)); 
        }
      public function viewtask3_paging($phong_ban,$creator_id,$date3,$date4,$page_start,$page_row)             // lay bao cao dan tiep cua tat ca quan ly phong truc thuoc  trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="select *,tasks.status,tasks.updated from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid and tasks.del_temporary=0 and  phong_ban.under=? and tasks.creator_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC limit $page_start,$page_row"; 
            return $db->fetchAll($sql,array($phong_ban,$creator_id,$date3,$date4)); 
        }
  /*    public function viewalltask3($phong_ban,$date3,$date4)        // lay bao cao dan tiep (cua quan ly phong truc thuoc) cua 1 user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="select *,tasks.status,tasks.updated from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid and tasks.del_temporary=0 and  phong_ban.under=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC";
            return $db->fetchAll($sql,array($phong_ban,$date3,$date4));   
        } */
      public function viewalltask3_num($phong_ban,$date3,$date4)        // lay bao cao dan tiep (cua quan ly phong truc thuoc) cua 1 user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="select count(*) as n from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid and tasks.del_temporary=0 and  phong_ban.under=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC";
            return $db->fetchRow($sql,array($phong_ban,$date3,$date4));   
        }
      public function viewalltask3_paging($phong_ban,$date3,$date4,$page_start,$page_row)        // lay bao cao dan tiep (cua quan ly phong truc thuoc) cua 1 user trong khoang? [date1,date2]
        {
            $db = $this->getSessionDB();
            $sql ="select *,tasks.status,tasks.updated from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid and tasks.del_temporary=0 and  phong_ban.under=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? ORDER BY tasks.date_start DESC limit $page_start,$page_row";
            return $db->fetchAll($sql,array($phong_ban,$date3,$date4));   
        }
      
      public function viewalltask4($uid,$creator_id,$status,$phong_ban,$date1,$date2,$page_start,$page_row)
        {
                     $db = $this->getSessionDB();
                     if($creator_id=='all' && $status=='all')
                         {
                          $sql="SELECT users_info.name,task_id , tasks.creator_id , task_title , date_start, date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid where tasks.del_temporary=0 and  phong_ban.under=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? 

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_start, date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? 

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_start, date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? 

                                 order by updated desc  limit $page_start,$page_row
                                ";   
                         }
                     if($creator_id!='all' && $status=='all')
                         {
                            $sql="SELECT users_info.name,task_id , tasks.creator_id , task_title , date_start, date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid where tasks.del_temporary=0 and  phong_ban.under=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? and tasks.creator_id=$creator_id

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_start, date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=?   and tasks.creator_id=$creator_id

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_start, date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=?   and tasks.creator_id=$creator_id

                                 order by updated desc limit $page_start,$page_row
                                "; 
                         }
                     if($creator_id=='all' && $status!='all')
                         {
                            $sql="SELECT users_info.name,task_id , tasks.creator_id , task_title , date_start, date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid where tasks.del_temporary=0 and  phong_ban.under=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? and tasks.status=$status

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_start, date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=?   and tasks.status=$status

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_start, date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=?   and tasks.status=$status

                                 order by updated desc limit $page_start,$page_row
                                "; 
                         }
                     if($creator_id!='all' && $status!='all')
                         {
                            $sql="SELECT users_info.name,task_id , tasks.creator_id , task_title , date_start, date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid where tasks.del_temporary=0 and  phong_ban.under=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? and tasks.status=$status   and tasks.creator_id=$creator_id

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_start, date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=?   and tasks.status=$status and tasks.creator_id=$creator_id

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_start, date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=?   and tasks.status=$status   and tasks.creator_id=$creator_id

                                 order by updated desc limit $page_start,$page_row
                                ";  
                         }
                     
                     
                     return   $db->fetchAll($sql,array($phong_ban,$date1,$date2,$uid,$date1,$date2,$uid,$date1,$date2));     
        }
        
        public function count_viewalltask4($uid,$creator_id,$status,$phong_ban,$date1,$date2)
        {
                     $db = $this->getSessionDB();
                     
                     if($creator_id=='all' && $status=='all')
                         {
                          $sql="
                          
                                select count(*) as n from(
                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid where tasks.del_temporary=0 and  phong_ban.under=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? 

                                                                union

                                                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? 

                                                                union

                                                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? 

                                                                 order by updated desc 

                                ) as tasks

                                ";   
                         }
                     if($creator_id!='all' && $status=='all')
                         {
                            $sql="                             
                               select count(*) as n from(  
                            SELECT users_info.name,task_id , tasks.creator_id , task_title , date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid where tasks.del_temporary=0 and  phong_ban.under=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? and tasks.creator_id=$creator_id

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=?   and tasks.creator_id=$creator_id

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=?   and tasks.creator_id=$creator_id

                                 order by updated desc 
                                 ) as tasks  
                                "; 
                         }
                     if($creator_id=='all' && $status!='all')
                         {
                            $sql="
                                select count(*) as n from( 
                            SELECT users_info.name,task_id , tasks.creator_id , task_title , date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid where tasks.del_temporary=0 and  phong_ban.under=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? and tasks.status=$status

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=?   and tasks.status=$status

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=?   and tasks.status=$status

                                 order by updated desc 
                                 ) as tasks 
                                "; 
                         }
                     if($creator_id!='all' && $status!='all')
                         {
                            $sql="
                                select count(*) as n from( 
                            SELECT users_info.name,task_id , tasks.creator_id , task_title , date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary from phong_ban inner join tasks inner join users_info on phong_ban.id_manager = tasks.creator_id and phong_ban.id_manager=users_info.uid where tasks.del_temporary=0 and  phong_ban.under=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=? and tasks.status=$status   and tasks.creator_id=$creator_id

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM permission inner join tasks inner join users_info on permission.creator_id=tasks.creator_id and tasks.creator_id=users_info.uid where permission.viewer_id=? and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=?   and tasks.status=$status and tasks.creator_id=$creator_id

                                union

                                SELECT users_info.name,task_id , tasks.creator_id , task_title , date_end , tasks.status , degree_complete , description , tasks.updated , del_temporary FROM tasks inner join phong_ban inner join users_info on phong_ban.phong_ban = users_info.phong_ban and users_info.uid=tasks.creator_id WHERE phong_ban.id_manager=?  and tasks.del_temporary=0 and tasks.date_start>= ? and tasks.date_start<=?   and tasks.status=$status   and tasks.creator_id=$creator_id

                                 order by updated desc 
                                 ) as tasks 
                                ";  
                         } 
                     
                     return   $db->fetchRow($sql,array($phong_ban,$date1,$date2,$uid,$date1,$date2,$uid,$date1,$date2));     
        }

}
